
export type Channel = {
  id: number
  name: string
}



export type NewsType = {
  art_id: string;
  title: string;
  aut_id: string;
  comm_count: number;
  pubdate: string;
  aut_name: string;
  is_top: number;
  cover: {
    type: 0 | 1 | 3;
    images: string[];
  }
}


export type NewsTypeLast = {
  [key: number]: NewsType[]
}

type SroolType = {
  [key: number]: number
}
//定义初始化函数
const initState = {
  list: [] as Channel[],
  alllist: [] as Channel[],
  activeId: 0,
  newsList: {} as NewsTypeLast,
  nowTime: {} as SroolType,
  sroolNum: {} as SroolType
}
type UserType =
  | { type: 'channel/saveList', payload: Channel[] }
  | { type: 'channel/saveAllList', payload: Channel[] }
  | { type: 'channel/saveNewList', payload: NewsType[] }
  | { type: 'channel/setActiveId', payload: number }
  | { type: 'channel/setSroolNum', payload: number }
  | { type: 'channel/setTime', payload: number }

export default function ChannelReducer(state = initState, action: UserType) {
  switch (action.type) {
    case 'channel/saveList':
      return { ...state, list: action.payload }
    case 'channel/saveAllList':
      return { ...state, alllist: action.payload }
    case 'channel/saveNewList':
      return { ...state, newsList: { ...state.newsList, [state.activeId]: action.payload } }
    case 'channel/setTime':
      return { ...state, nowTime: { ...state.nowTime, [state.activeId]: action.payload } }
    case 'channel/setActiveId':
      return { ...state, activeId: action.payload }
    case 'channel/setSroolNum':
      return { ...state, sroolNum: { ...state.sroolNum, [state.activeId]: action.payload } }
    default:
      return state
  }
}